<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>{{ title() }}</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px" :rules="addRules" :model="addForm">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" />
            </el-form-item>
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" />
            </el-form-item>
            <el-form-item label="所在行业" prop="industryCode">
              <el-select v-model="addForm.industryCode">
                <el-option
                  v-for="item in industryList"
                  :key="item.industryCode"
                  :value="item.industryCode"
                  :label="item.industryName"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="营业执照" prop="businessLicenseId">
              <el-upload
                action="#"
                :file-list="fileList"
                :http-request="uploadRequest"
                :before-upload="beforeUpload"
              >
                <el-button v-if="fileList.length===0" plain size="small" type="primary">点击上传</el-button>
                <el-button v-else size="small" type="success">已上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过 5MB</div>
              </el-upload>
            </el-form-item>
          </el-form>

        </div>
      </div>
      <div class="form-container">
        <div class="title">联系人信息</div>
        <div class="form">
          <el-form ref="ruleForm2" label-width="100px" :rules="addRules" :model="addForm">
            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="addForm.contact" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" />
            </el-form-item>
          </el-form>

        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button>重置</el-button>
        <el-button type="primary" @click="confirmSubmit">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { getIndustryListAPI, uploadAPI, createEnterpriseAPI, getEnterpriseDetailAPI, updateEnterpriseAPI } from '@/api/enterprise'

export default {
  data() {
    return {
      fileList: [],

      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '', // 营业执照url
        businessLicenseId: '' // 营业执照id
      },
      addRules: {
        name: [
          { required: true, message: '企业名称为必填', trigger: 'blur' }
        ],
        legalPerson: [
          { required: true, message: '法人为必填', trigger: 'blur' }
        ],
        registeredAddress: [
          { required: true, message: '注册地址为必填', trigger: 'blur' }
        ],
        industryCode: [
          { required: true, message: '所在行业为必填', trigger: 'change' }
        ],
        contact: [
          { required: true, message: '企业联系人为必填', trigger: 'blur' }

        ],
        contactNumber: [
          { required: true, message: '企业联系人电话为必填', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur' }
        ],
        businessLicenseId: [
          { required: true, message: '请上传营业执照', trigger: 'blur' }
        ]
      },
      industryList: []
    }
  },
  created() {
    this.getIndustryList()
    this.getEnterpriseDetail()
  },
  methods: {
    async  getEnterpriseDetail() {
      if (this.$route.query.id) {
        const res = await getEnterpriseDetailAPI(this.$route.query.id)
        this.addForm = res.data
        console.log(this.addForm)
      }
    },
    title() {
      return this.$route.query.id ? '编辑企业' : '新增企业'
    },
    async confirmSubmit() {
      const valid1 = await this.$refs.ruleForm.validate().then(valid => valid)
      const valid2 = await this.$refs.ruleForm2.validate().then(valid => valid)

      if (valid1 && valid2) {
        // console.log(this.addForm)
        if (this.$route.query.id) {
          // console.log(1)
          console.log(this.$route.query.id)
          delete this.addForm.businessLicenseName
          delete this.addForm.industryName
          delete this.addForm.rent

          await updateEnterpriseAPI({ ...this.addForm, id: this.$route.query.id })
        } else {
          await createEnterpriseAPI(this.addForm)
        }

        // 返回列表页
        this.$router.back()
      }
    },

    async uploadRequest(data) {
      const file = data.file

      // 处理formData类型参数
      const formData = new FormData()
      formData.append('file', file)
      formData.append('type', 'businessLicense')
      const res = await uploadAPI(formData)

      // 赋值表单数据
      this.addForm.businessLicenseId = res.data.id
      this.addForm.businessLicenseUrl = res.data.url

      // el-upload 显示已上传文件列表
      // el-upload 组件要求列表对象必须有 name 和 url 属性才可以
      this.fileList.push({
        name: file.name,
        url: res.data.url
      })
      this.$refs.ruleForm.validateField('businessLicenseId')
    },
    beforeUpload(file) {
      const allowImgType = ['image/jpeg', 'image/png', 'image/jpg'].includes(file.type)
      const isLt5M = file.size / 1024 / 1024 < 5

      if (!allowImgType) {
        this.$message.error('上传营业执照图片只能是 PNG/JPG/JPEG 格式!')
      }
      if (!isLt5M) {
        this.$message.error('上传营业执照图片大小不能超过 5MB!')
      }
      return allowImgType && isLt5M
    },
    submitUpload() {
      this.$refs.upload.submit()
    },
    handleRemove(file, picture) {
      console.log(file, picture)
    },
    handlePreview(file) {
      console.log(file)
    },
    async getIndustryList() {
      const res = await getIndustryListAPI()
      this.industryList = res.data
    }
  }
}
</script>

<style scoped lang="scss">
.add-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left:20px;
      }
      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form{
          display: flex;
          flex-wrap: wrap;
          .el-form-item{
            width: 50%;
          }
        }
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
